package com.wwtech.epp.ui.activity.work.presenter;

import android.content.Intent;
import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.AttachmentBean;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.entity.LoadFile;
import com.wwtech.epp.ui.activity.work.DynamicAppActivity;
import com.wwtech.epp.ui.activity.work.NewProjectActivity;
import com.wwtech.epp.ui.activity.work.contract.DynamicAppContract;
import com.wwtech.epp.utils.ToastUtil;

import java.io.File;
import java.util.HashSet;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.rong.imkit.model.FileInfo;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import static com.wwtech.epp.ui.activity.work.NewProjectActivity.RESULTOK;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of DynamicAppActivity
 * @date 2017/09/18 21:54:46
 */
public class DynamicAppPresenter implements DynamicAppContract.DynamicAppContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final DynamicAppContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private DynamicAppActivity mActivity;

    @Inject
    public DynamicAppPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull DynamicAppContract.View view, DynamicAppActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getUIDesc(String dataId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getUIDesc(dataId, map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        ToastUtil.displayShortToast(baseBack.getResultMsg());
                        if (baseBack.getResultCode() == 0) {
                            AppConfig.getInstance().mAppActivityManager.finishActivity(NewProjectActivity.class);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == NewProjectActivity.CHECKPART && resultCode == RESULTOK) {
            mView.onSetData(data.getStringExtra("key"));
        }

        if (requestCode == 730) {
            if (data == null) {
                return;
            }
            HashSet<FileInfo> files = (HashSet<FileInfo>) data.getSerializableExtra("selectedFiles");
            for (FileInfo file : files) {
                KLog.i(file.getFilePath());
                KLog.i(file.getFileName());
                AttachmentBean attachmentBean = new AttachmentBean();
                attachmentBean.setLocalUrl(file.getFilePath());
                attachmentBean.setName(file.getFileName());
                attachmentBean.setState(AttachmentBean.State.select);
                upLoadFile(attachmentBean);
                return;
            }
        }
    }

    private void upLoadFile(AttachmentBean attachmentBean) {
        mView.showProgressDialog();
        RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), new File((attachmentBean.getLocalUrl())));
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("filename", attachmentBean.getName(), fileRequestBody);
        Disposable disposable = httpAPIWrapper.uploadFile(filePart)
                .subscribe(new Consumer<LoadFile>() {
                    @Override
                    public void accept(LoadFile loadFile) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        attachmentBean.setUrl(loadFile.getData().getUrl());
                        mView.loadFileSuccess(attachmentBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        mView.closeProgressDialog();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void upLoadData(Map map) {
        KLog.i(map);
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.upLoadAppData( map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        ToastUtil.displayShortToast("提交成功");
                        if (baseBack.getResultCode() == 0) {
                            AppConfig.getInstance().mAppActivityManager.finishActivity(DynamicAppActivity.class);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        mView.closeProgressDialog();
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}